import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;

/**
 * This is the class that sets up the board. It has buttons for each
 * place you can go and a listener for the row that is selected and the column
 * that is selected.
 * @author Josiah Hotovec
 */
public class GUIBoard2 extends JFrame
{
    /**
	 * 
	 */
private static final long serialVersionUID = 3012276961047068949L;
	//Variables
    private static final int FRAME_WIDTH = 600;
    private static final int FRAME_HIGHT = 400;
    private int _column = -1;
    private int _row = -1;
    private int _tile = 0;

    private JButton[][] _buttons = new JButton[8][8];
    private JButton _draw = new JButton("Draw Tile");
    private JButton _endTurn = new JButton("End Turn");

    public Hand hand = new Hand();

    private JTextField _score;
    private JTextField _tileCount;
    private JTextField _turn;

    ActionListener boardListener = new boardListener();
    ActionListener drawListener = new drawListener();
    ActionListener endTurnListener = new endTurnListener();

    MasterControl master;

    Container _pane = getContentPane();

        public static void main(String[] args)
    {
        PlayerCount counter = new PlayerCount();

        while(counter.getPlayerCount() < 1)
        {

        }

        counter.setVisible(false);

        GUIBoard2 board = new GUIBoard2(counter.getPlayerCount());
        board.setTitle("Quirks");

    }

    /**
     * This initializes the board. It sets all the buttons and adds the
     * listeners
     */
    public GUIBoard2(int count)
    {
        JPanel board = new JPanel();
        _score = new JTextField();
        _tileCount = new JTextField();
        _turn = new JTextField();
        _draw.addActionListener(drawListener);
        master = new MasterControl(count);

        setSize(FRAME_WIDTH, FRAME_HIGHT);
        addWindowListener(new WindowCloser());
        board.setLayout(new GridLayout(8, 8));

        /*
         * These nested for loops set up an array of arrays that hold the
         * buttons for the board. It also applies the listeners to each button.
         * Finally it adds each button to the board so that each button is on
         * the row corresponding and column for each listener attached.
         */
        for(int i = 1; i < 8; i++)
        {
            for(int j = 1; j < 8; j++)
            {
                // The switch sets the row listener on the correct button based
                // on the row it is in.
                switch(i)
                {
                    case 1: _buttons[i][j] = new JButton("A " + j);
                            _buttons[i][j].addActionListener(boardListener);
                            _buttons[i][j].setName(i + ", " + j); break;

                    case 2: _buttons[i][j] = new JButton("B " + j);
                            _buttons[i][j].addActionListener(boardListener);
                            _buttons[i][j].setName(i + ", " + j); break;

                    case 3: _buttons[i][j] = new JButton("C " + j);
                            _buttons[i][j].addActionListener(boardListener);
                            _buttons[i][j].setName(i + ", " + j); break;

                    case 4: _buttons[i][j] = new JButton("D " + j);
                            _buttons[i][j].addActionListener(boardListener);
                            _buttons[i][j].setName(i + ", " + j); break;

                    case 5: _buttons[i][j] = new JButton("E " + j);
                            _buttons[i][j].addActionListener(boardListener);
                            _buttons[i][j].setName(i + ", " + j); break;

                    case 6: _buttons[i][j] = new JButton("F " + j);
                            _buttons[i][j].addActionListener(boardListener);
                            _buttons[i][j].setName(i + ", " + j); break;

                    case 7: _buttons[i][j] = new JButton("G " + j);
                            _buttons[i][j].addActionListener(boardListener);
                            _buttons[i][j].setName(i + ", " + j); break;
                }

                    board.add(_buttons[i][j]);
            }
        }

        setBoard();
        _pane.setLayout(new FlowLayout());
        _pane.add(board);
        _pane.add(hand);
        _pane.add(_draw);
        _pane.add(_endTurn);
        _pane.add(_score);
        _pane.add(_tileCount);
        _pane.add(_turn);

        setScore();
        setTileCount();
        setTurn();
        setHand();

        setVisible(true);
    }

    private void setBoard()
    {
        int[][] boardTracker = master.getBoard();

        for(int i = 1; i < 8; i++)
        {
            for(int j = 1; j < 8; j++)
            {
              _buttons[i][j].setText(Integer.toString(boardTracker[i][j]));
            }
        }
    }


    /**
     * This is sent the number of the tile that is selected and stores it so
     * that if the move can be made the button on the board can be changed to
     * reflect that.
     * @param tile
     */
    public void setTile(int tile)
    {
        _tile = tile;
        makeMove();
    }

    @SuppressWarnings("deprecation")
	public void makeMove()
    {
        boolean middle = _row == 4 && _column == 4;

        if(_row != -1 && _column != -1 && _tile != -1 &&
                !(_buttons[_row][_column].getText().charAt(0) > 49 &&
                _buttons[_row][_column].getText().charAt(0) < 59)
                && !middle)
        {
            if(master.makeMove(_row, _column, _tile) != -1);
            {
                _row = -1;
                _column = -1;
                _tile = -1;

                setBoard();
                setScore();
                setHand();
            }
        }

        System.out.println("Make move -- row: " + _row + "col: " + _column);
    }

    public void draw()
    {
        master.drawTile();
        setHand();
        setTileCount();
        endTurn();
    }

    public void endTurn()
    {
        master.endTurn();
        setScore();
        setTurn();
        setHand();
    }

    public void setHand()
    {
        ArrayList <Integer> temp = new ArrayList<Integer>();

        temp = master.getHand();

        hand.setHand(temp);
    }

    public void setScore()
    {
        _score.setText("score: " + master.getPlayerScore());
    }

    public void setTileCount()
    {
        _tileCount.setText("Tiles remaining: " + master.getRemainingTiles());
    }

    public void setTurn()
    {
        _turn.setText("Player " + (master.getPlayer() + 1));
    }

    /**
     * Ends the program when the window is closed.
     */
    private class WindowCloser extends WindowAdapter
    {
        @Override
        public void windowClosing(WindowEvent event)
        {
            System.exit(0);
        }
    }

   public class boardListener implements ActionListener
   {
        public void actionPerformed(ActionEvent ae)
        {
            JButton temp = (JButton) ae.getSource();
            char tempRow;
            char tempCol;

            tempRow = temp.getName().charAt(0);
            _row = (tempRow % 48);

            tempCol = temp.getName().charAt(3);
            _column = (tempCol % 48);

            if(_tile > 0)
            {
            	makeMove();
            }
        }
   }

   public class drawListener implements ActionListener
   {
        public void actionPerformed(ActionEvent ae)
        {
            draw();
        }
   }

      public class endTurnListener implements ActionListener
   {
        public void actionPerformed(ActionEvent ae)
        {
            endTurn();
        }
   }

    public class Hand extends JPanel
    {
        /**
		 * 
		 */
        private static final long serialVersionUID = 7552164871886846827L;
        //int _tile = -1;
        JPanel _handPanel;
        JButton[] _tiles;

    public Hand()
    {
        ActionListener handListener = new handListener();

        _tiles = new JButton[5];

        _handPanel = new JPanel();

        setLayout(new GridLayout(1, 5));

        for(int i = 0; i < 5; i++)
        {
            _tiles[i] = new JButton("hand " + i);

            _tiles[i].addActionListener(handListener);
            _tiles[i].setText("" + 0);

            add(_tiles[i]);
        }
    }

    public void setHand(ArrayList<Integer> tiles)
    {
        for(int i = 0; i < tiles.size(); i ++)
        {
            _tiles[i].setText("" + (int)tiles.get(i));
        }
    }

    private class handListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            JButton temp = (JButton) event.getSource();
            _tile = (int)temp.getText().charAt(0);
            if(49 == _tile && temp.getText().endsWith("0"))
            {
                _tile = 58;
            }
            _tile = _tile - 48;
        }
    }
}
  
}

